package org.opens.javaskill.thread.create;

public class CreateDaemonThread {

    /**
     * 说明:
     *      1. 运行这个程序会发现, main线程并没有等待thread线程就自己结束了.
     *          - 输出:
     *          ```
     *          main线程执行了最后一条语句
     *          ```
     *      2. 但是调整`thread.setDaemon(false);`后, 就会发现main线程会等待thread线程结束而结束.
     *          - 输出:
     *          ```
     *          main线程执行了最后一条语句
     *          多线程sleep完毕
     *          ```
     *
     */
    public static void main(String[] args) {
        Thread thread = new Thread(() -> {
            try {
                Thread.sleep(1000);
                System.out.println("多线程sleep完毕");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        // 设置thread线程为守护线程
        thread.setDaemon(false);
        thread.start();
        System.out.println("main线程执行了最后一条语句");
    }

}

class CreateDaemonThread2 {

    public static void main(String[] args) {
        System.out.println(Thread.currentThread().isDaemon());
    }

}

class CreateDaemonThread3 {

    /**
     * 说明:
     *      1. 调整thread和innerThread的线程类型来观察输出;
     *          - 某些情况需要搭配调整main, thread, innerThread三个线程的沉睡时间来观察输出;
     */
    public static void main(String[] args) {
        Thread thread = new Thread(() -> {
            Thread innerThread = new Thread(() -> {
                try {
                    Thread.sleep(10000);
                    System.out.println("线程innerThread finish done.");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
            innerThread.setDaemon(false);
            innerThread.start();
            try {
                Thread.sleep(5000);
                System.out.println("线程thread finish done.");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        thread.setDaemon(false);
        thread.start();
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("线程main finish done.");
    }

}

class DaemonHeartbeatCheckThread {

    public static void main(String[] args) {
        Thread thread = new Thread(() -> {
            Thread heartbeatCheckThread = new Thread(() -> {
                try {
                    while (true) {
                        System.out.println("线程heartbeatCheckThread 发起心跳检测.");
                        Thread.sleep(500);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
            heartbeatCheckThread.setDaemon(true);
            heartbeatCheckThread.start();
            try {
                Thread.sleep(5000);
                System.out.println("线程thread finish done.");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        thread.setDaemon(false);
        thread.start();
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("线程main finish done.");
    }

}